package f.h.dragon.util;

import static f.h.dragon.util.Arrays.*;

import java.util.Arrays;

/**
 * Random: 随机数，提供随机数相关操作
 *
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/02/05
 * @since 1.0
 */
public class Random {
	
	//local variables

	//Constructor
	/**
	 * Create a empty Random 
	 */
	public Random(){
		
	}
	
	//Logic
	/**
	 * 生成count个介于min(包含)和max(不包含)之间的随机数
	 * 
	 * @param min - the min of random
	 * @param max - the max of random
	 * @param count - the count of random
	 * @return
	 * @see {@link RandomUtil#randoms(int, int, int)}
	 */
	public int[] nexts(int min, int max, int count){
		//range check
		if(min < 0 || max < min || count < 1 || count > max - min){
			throw new IllegalArgumentException("min=" + min + ", max=" + max + ", count=" + count);
		}
		
		//init randoms
		int length = max - min;
		int[] randoms = new int[length];
		for(int i = 0, counti = length; i < counti; i++){
			randoms[i] = min + i;
		}
		
		//make randoms
		java.util.Random random = new java.util.Random();
		for(int i = 0; i < count; i++){
			swap(randoms, i, random.nextInt(length));
		}
		
		return Arrays.copyOf(randoms, count);
	}
	
	/**
	 * test...
	 * 
	 * @param args
	 */
	public static void main(String[] args){
		long st = System.nanoTime();
		int min = 1, max = 21000000, count = 20000000;
		Random r = new Random();
		int[] rs = r.nexts(min, max, count);
		System.out.println("make randoms: " + (System.nanoTime() - st));
		System.out.println(rs);
//		int[] sr = new int[max];
//		for(int i : rs){
//			sr[i] = i;
//		}
//		System.out.println("sort randoms: " + (System.nanoTime() - st));
//		for(int i : sr){
//			if(i != 0){
//				System.out.print(i + ",");
//			}
//		}
//		System.out.println("out randoms: "+ (System.nanoTime() - st));
	}
	
}
